﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalaryChannel.SCUserService;

namespace SalaryChannel
{
    public partial class MainPage : UserControl
    {
        private Control _m_original;

        public Control Original
        {
            get { return _m_original; }
            set
            {
                _m_original = value;
                //_m_original.Visibility = Visibility.Visible;
                try
                {
                    Storyboard d = this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS);
                    d.Completed += (s, e) =>
                        {
                            d.Stop();
                        };
                    d.Begin();
                }
                catch 
                {
                }
            }
        }

        private const string GROW = "pic_Grow";
        private const string SLIDE_UP = "pic_SlideUp";
        private const string SLIDE_DOWN = "pic_SlideDown";
        private const string SLIDE_RIGHT = "pic_SlideRight";
        private const string SLIDE_LEFT = "pic_SlideLeft";
        private const string WIPE_IN = "pic_WipeIn";
        private const string FILL_LEFT = "pic_FillFromLeft";

        //fades
        private const string FADEIN = "pic_fadeIn";
        private const string FADEOUT = "pic_fadeOut";
        private const double FADESECONDS = 0.5;

        // this is used if we need to use the imgPicture_SizeChanged event handler to start the storyboard
        private string m_curAction = string.Empty;

        // this is used for fadein/fadeout because out attempts to get the animation target after the fade out
        // to pass it to the fadein storyboard returns null
        // private Image m_fadeTarget;
        private delegate Storyboard StoryboardDelegate(Control c, double d1, double d2, double s);

        private StoryboardDelegate[] storyBoards;
       
        
        static Random rSb = new Random();


    static string logoffStr = Properties.Resource.Logoff;
    static string loginStr = Properties.Resource.LoginTitle;
    IDictionary<string, string> _params;

        public MainPage()
        {
            InitializeComponent();            
          
        }

        public MainPage(IDictionary<string, string> dicParam)
        {
            InitializeComponent();
            _params = dicParam;


            try
            {
                SLTitleTB.Text = _params["sl_title"].ToString();
            }
            catch (KeyNotFoundException e)
            {
                SLTitleTB.Text="欢迎使用移动渠道综合支撑系统";
            }
            this.storyBoards = new StoryboardDelegate[]
            {
                PrepareWipeInStoryBoard,
                PrepareGrowStoryBoard,
                PrepareSlideInFromLeftStoryBoard,
                PrepareSlideUpStoryBoard,
                PrepareSlideDownStoryBoard,
                PrepareSlideInFromRightStoryBoard,
                PrepareSlideInFromLeftStoryBoard

            };

            this.Loaded += new RoutedEventHandler(Page_Loaded);
            this.m_login.L_Action += Logon;

            this.m_inforBox.MouseLeftButtonDown += new MouseButtonEventHandler(m_inforBox_MouseLeftButtonDown);
            
        }



        private bool IsInforBoxIn = false;

        void m_inforBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsInforBoxIn)
            {
                InfoOutSB.Begin();
                IsInforBoxIn = true;
            }
            else
            {
                InfoInSB.Begin();
                IsInforBoxIn = false;
            }
        }

       

        
        public void Logon()
        {
            this.btnLogin.Content = logoffStr;           

            try
            {

                UserInfoEntity user = App.CurrentUser;
                string level = "个人权限";
                if (App.IsAuth(13))
                {
                    level = "市级权限";
                }
                else if (App.IsAuth(12))
                {
                    level = "县级权限";
                }
                else if (App.IsAuth(11))
                {
                    level = "片区权限";
                }
                else if (App.IsAuth(10))
                {
                    level = "县级权限";
                }
                this.UserLabel.Text = string.Format("{3}{0}        {4}[{1}]{2}    您拥有：{5}",
                    user.UserName, user.ChannelID, user.ChannelName, 
                    Properties.Resource.ID_, Properties.Resource.Channel_,level);

                foreach (Control c in stackOptions.Children)
                {
                    c.Visibility = Visibility.Visible;
                }

                //this.btnUpload.Visibility = App.IsAuth(2)? Visibility.Visible: Visibility.Collapsed;
                this.btnPulicInfor.Visibility = App.IsAuth(3) ? Visibility.Visible : Visibility.Collapsed;
                this.btnAddUser.Visibility = App.IsAuth(4) ? Visibility.Visible : Visibility.Collapsed;
                //this.btnQL.Visibility = App.IsAuth(5) ? Visibility.Visible : Visibility.Collapsed;

              //  btnTaskMngmt.Visibility = App.IsAuth(17) ? Visibility.Visible : Visibility.Collapsed;
               // btnMyJob.Visibility = App.IsAuth(17) ? Visibility.Visible : Visibility.Collapsed;

                if (App.IsAuth(21,30))
                {
                    btnPSS.Visibility = Visibility.Visible;
                }
                else
                {
                    btnPSS.Visibility = Visibility.Collapsed;
                }

              
                this.m_inforBox_MouseLeftButtonDown(null, null);
                if (App.CurrentUser.Tel == null
                    || App.CurrentUser.Email == null
                    || App.CurrentUser.Password.Length<8)
                {
                    this.btnChangePwd_Click(null, null);
                    if(App.CurrentUser.Password.Length>=8)
                    {
                    App.MessageBox.ShowDialog("请立即修改您的手机和邮箱");
                    }
                    else if (!(App.CurrentUser.Tel == null
                    || App.CurrentUser.Email == null))
                    {
                        App.MessageBox.ShowDialog("请立即修改您的密码");
                    }
                    else
                    {
                        App.MessageBox.ShowDialog("请立即修改您的密码和联系信息");
                    }
                }


            }
            catch 
            {
                App.MessageBox.ShowDialog("初始化界面失败");
            }
           
        }
        private StoryboardDelegate RandomStoryboard()
        {
            int index = rSb.Next(0, this.storyBoards.Length);
            return storyBoards[index];

        }
        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            //imgPicture.Source = m_original;
            //// just in case the animation hasn't stopped yet....
            //imgPicture.Clip = null;
        }

        private void m_original_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Control img = sender as Control;
            if (m_curAction == WIPE_IN)
            {
                Storyboard sb = PrepareWipeInStoryBoard(img, e.NewSize.Width, e.NewSize.Height, 2);
                sb.Begin();
                m_curAction = string.Empty;
                return;
            }
            //if (m_curAction == FILL_LEFT)
            //{
            //    Storyboard sb = PrepareFillInFromLeftStoryBoard(img, e.NewSize.Width, e.NewSize.Height, 2);
            //    sb.Begin();
            //    m_curAction = string.Empty;
            //    return;
            //}
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("Loaded");
            //m_login = new Login();
            //m_login.VerticalAlignment = VerticalAlignment.Stretch;
           
            
            _m_original = new UserControl();
            _m_original.SizeChanged += new SizeChangedEventHandler(m_original_SizeChanged);


            try
            {
                if (Browser.ScreenHeight < 800.0)
                {
                    App.MessageBox.ShowDialog("如果界面没有完全展示，可以按F11进入全屏模式，再按F11将退出全屏");
                }
            }
            catch 
            {
                App.MessageBox.ShowDialog("如果界面没有完全展示，可以按F11进入全屏模式，再按F11将退出全屏");
            }

            OOBInstall oob = new OOBInstall();
            oob.Update();

        }

        private void LogoffAction()
        {
            foreach (Control c in ContentGrid.Children)
            {
                c.Visibility = Visibility.Collapsed;
            }
            foreach (Control c in stackOptions.Children)
            {
                c.Visibility = Visibility.Collapsed;
            }

            this.btnLogin.Content = loginStr;
            this.UserLabel.Text = string.Empty;
            btnLogin.Visibility = Visibility.Visible;
            m_login.PwdTB.Password = string.Empty;
            m_login.IDTB.Text = string.Empty;

            App.CurrentUser = null;
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }

            if (this.btnLogin.Content.Equals(logoffStr))
            {
                App.MessageBox.ShowDialog(Properties.Resource.LogoffWarning, new Action(LogoffAction), Title.Information);

                return;
            }
            // login
            foreach (Control c in ContentGrid.Children)
            {
                c.Visibility = Visibility.Collapsed;
            }
            m_login.Visibility = Visibility.Visible;
            m_login.IDTB.Text = string.Empty;
            m_login.PwdTB.Password = string.Empty;
            m_login.statusTB.Text = string.Empty;
            _m_original = m_login;
            Original = m_login;
            //m_login.L_Action = loggedin;
            //Storyboard sb = this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS);
            ////imgPicture.Source = imgThumb.Source;
            //sb.Begin();
        }

        private void loggedin()
        {
            this.btnLogin.Content = logoffStr;

        }
        //private void btnQuery_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    m_query.Visibility = Visibility.Visible;

        //    m_query.Initialize();
        //    Original = m_query;
        //    //_m_original = m_query;
        //    //this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS).Begin();
        //}
        //private void btnUpload_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;

        //    m_upload.Visibility = Visibility.Visible;
        //    m_upload.FileTB.Text = string.Empty;

        //    Original = m_upload;
        //    //_m_original = m_upload;
        //    //this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS).Begin();
        //}

        private void btnChangePwd_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }
            Original.Visibility = Visibility.Collapsed;
            this.m_PwdCtrl.Visibility = Visibility.Visible;
            this.m_PwdCtrl.Initiallize();
            Original = m_PwdCtrl;
            //_m_original = m_PwdCtrl;
            //this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS).Begin();
        }


        private void btnAddUser_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }

            Original.Visibility = Visibility.Collapsed;
            this.m_UserMG.Visibility = Visibility.Visible;
            this.m_UserMG.Initailize();
            Original = m_UserMG;
            //_m_original = m_UserMG;
            //this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS).Begin();
        }
        
        private void btnPulicInfor_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }
            Original.Visibility = Visibility.Collapsed;
            this.m_PublicInforAdd.Visibility = Visibility.Visible;
            m_PublicInforAdd.ExpireDate.Text = DateTime.Now.ToShortDateString();
            m_PublicInforAdd.BodyTB.Text = string.Empty;
            Original = m_PublicInforAdd;
            //_m_original = m_PublicInforAdd;
            //this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS).Begin();
        }
        ///// <summary>
        ///// 打开意见界面
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void btnAdvise_Click(object sender, RoutedEventArgs e)
        //{
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_AdviseCtrl.Visibility = Visibility.Visible;
        //    this.m_AdviseCtrl.ClearUI();
        //    Original = m_AdviseCtrl;

        //}
        //private void btnQL_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_QL.Visibility = Visibility.Visible;
        //    this.m_QL.ResultGrid.ItemsSource = null;
        //    Original = m_QL;
        //    //_m_original = m_QL;
        //    //this.RandomStoryboard()(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, FADESECONDS).Begin();
        //}
        //private void btnChannelView_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_View.Visibility = Visibility.Visible;
        //    m_View.Initialize();
        //   // this.m_QL.ResultGrid.ItemsSource = null;
        //    Original = m_View;
        //}

        /// <summary>
        /// 我的任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnMyJob_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_MyJob.Visibility = Visibility.Visible;
        //    this.m_MyJob.Initiallize();
        //    Original = m_MyJob;
        //}

        //private void btnTaskMngmt_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_TaskMngmt.Initiallize();
        //    this.m_TaskMngmt.Visibility = Visibility.Visible;
        //    Original = m_TaskMngmt;

        //}
        /// <summary>
        /// 业务信息收集平台
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnInforCollect_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_InforCollectCtrl.Initiallize();
        //    this.m_InforCollectCtrl.Visibility = Visibility.Visible;
        //    Original = m_InforCollectCtrl;
        //}

        /// <summary>
        /// 渠道报表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }
            Original.Visibility = Visibility.Collapsed;
            this.m_Report.Initiallize();
            this.m_Report.Visibility = Visibility.Visible;
            Original = m_Report;
        }

        /// <summary>
        /// 营业厅综合支撑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSupport_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }

            Original.Visibility = Visibility.Collapsed;
            this.m_Support.Initiallize();
            this.m_Support.Visibility = Visibility.Visible;
            Original = m_Support;
        }

        /// <summary>
        /// 终端预约
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnOrder_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_Order.Initiallize();
        //    this.m_Order.Visibility = Visibility.Visible;
        //    Original = m_Order;
        //}

        /// <summary>
        /// 供销管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnbtnPSS_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }
            try
            {

                Original.Visibility = Visibility.Collapsed;
                this.m_PSS.Initiallize();
                this.m_PSS.Visibility = Visibility.Visible;
                Original = m_PSS;
            }
            catch (Exception ex)
            {
                App.MessageBox.ShowDialog(ex.Message);
            }
        }

        private void btnPrize_Click(object sender, RoutedEventArgs e)
        {
            if (App.IsRunning)
            {
                App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
                return;
            }
            try
            {

                Original.Visibility = Visibility.Collapsed;
                this.m_Prize.Initiallize();
                this.m_Prize.Visibility = Visibility.Visible;
                Original = m_Prize;
            }
            catch (Exception ex)
            {
                App.MessageBox.ShowDialog(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnWiredNet_Click(object sender, RoutedEventArgs e)
        //{
        //    if (App.IsRunning)
        //    {
        //        App.MessageBox.ShowDialog("当前有未返回的请求，请耐心等待");
        //        return;
        //    }
        //    Original.Visibility = Visibility.Collapsed;
        //    this.m_WiredNet.Initiallize();
        //    this.m_WiredNet.Visibility = Visibility.Visible;
        //    Original = m_WiredNet;
        //}


        #region Grow Storyboard

        private Storyboard PrepareGrowStoryBoard(Control aControl, double picWidth, double picHeight, double seconds)
        {
            Storyboard sb = null;
            if (LayoutRoot.Resources.Contains(GROW))
                sb = LayoutRoot.Resources[GROW] as Storyboard;
            if (sb != null)
                LayoutRoot.Resources.Remove(GROW);
            sb = new Storyboard();
            LayoutRoot.Resources.Add(GROW, sb);
            sb.Duration = new Duration(TimeSpan.FromSeconds(seconds));

            TransformGroup tGroup = new TransformGroup();
            ScaleTransform st = new ScaleTransform();
            tGroup.Children.Add(st);
            st.CenterX = 0.5;
            st.CenterY = 0.5;
            aControl.RenderTransform = tGroup;
            aControl.RenderTransformOrigin = new Point(0.5, 0.5);

            DoubleAnimation dX = new DoubleAnimation();
            dX.From = 0;
            dX.To = 1;
            dX.FillBehavior = FillBehavior.HoldEnd;
            sb.Children.Add(dX);

            DoubleAnimation dY = new DoubleAnimation();
            dY.From = 0;
            dY.To = 1;
            dY.FillBehavior = FillBehavior.HoldEnd;
            sb.Children.Add(dY);

            Storyboard.SetTarget(dX, aControl);
            Storyboard.SetTargetProperty(dX,
               new PropertyPath("(UIElement.RenderTransform).(tGroup.Children)[0].(ScaleTransform.ScaleX)"));
            Storyboard.SetTarget(dY, aControl);
            Storyboard.SetTargetProperty(dY,
               new PropertyPath("(UIElement.RenderTransform).(tGroup.Children)[0].(ScaleTransform.ScaleY)"));

            sb.Completed += (s, e) =>
            {
                sb.Stop();
            };
            return sb;
        }
      
        
        #endregion Grow Storyboard

        #region Wipe In Storyboard

        private Storyboard PrepareWipeInStoryBoard(Control control, double picWidth, double picHeight, double seconds)
        {
            Storyboard sb = null;
            if (LayoutRoot.Resources.Contains(WIPE_IN))
                sb = LayoutRoot.Resources[WIPE_IN] as Storyboard;
            if (sb != null)
                LayoutRoot.Resources.Remove(WIPE_IN);

            sb = new Storyboard();
            LayoutRoot.Resources.Add(WIPE_IN, sb);

            EllipseGeometry elGeom = new EllipseGeometry();
            elGeom.Center = new Point(picWidth / 2, picHeight / 2);
            control.Clip = elGeom;

            DoubleAnimation dX = new DoubleAnimation();
            dX.From = 0;
            dX.To = picWidth;
            dX.FillBehavior = FillBehavior.HoldEnd;
            dX.Duration = new Duration(TimeSpan.FromSeconds(seconds));

            sb.Children.Add(dX);

            Storyboard.SetTarget(dX, control);
            Storyboard.SetTargetProperty(dX,
               new PropertyPath("(UIElement.Clip).(EllipseGeometry.RadiusX)"));

            DoubleAnimation dY = new DoubleAnimation();
            dY.From = 0;
            dY.To = picHeight;
            dY.FillBehavior = FillBehavior.HoldEnd;
            dY.Duration = new Duration(TimeSpan.FromSeconds(seconds));
            sb.Children.Add(dY);

            Storyboard.SetTarget(dY, control);
            Storyboard.SetTargetProperty(dY,
               new PropertyPath("(UIElement.Clip).(EllipseGeometry.RadiusY)"));

            sb.Completed += new EventHandler(WipeIn_Completed);
            return sb;
        }

        private void WipeIn_Completed(object sender, EventArgs e)
        {

            Storyboard sb = sender as Storyboard;
            if (sb != null)
            {
                sb.Stop();
                LayoutRoot.Resources.Remove(WIPE_IN);
                m_curAction = string.Empty;
            }
            _m_original.Clip = null;
        }

        private void WipeIn_Click(object sender, RoutedEventArgs e)
        {
            double oldRatio = _m_original.ActualWidth / _m_original.ActualHeight;
            //double newRatio = imgThumb.ActualWidth / imgThumb.ActualHeight;
            //m_original.Source = imgThumb.Source;
            //double diff = Math.Abs(newRatio - oldRatio);
           // if (diff < 0.01)
            //{
                Storyboard sb = PrepareWipeInStoryBoard(_m_original, _m_original.ActualWidth, _m_original.ActualHeight, 0.5);
                sb.Begin();
            //}
            //else
            //{
            //    // do it in the image resize event
            //    m_curAction = WIPE_IN;
            //}
        }

        #endregion Wipe In Storyboard      

       
        
        #region Slide Up Storyboard

        private Storyboard PrepareSlideUpStoryBoard(Control control,double w,double h, double seconds)
        {
          Storyboard sb = null;
          if (LayoutRoot.Resources.Contains(SLIDE_UP))
            sb = LayoutRoot.Resources[SLIDE_UP] as Storyboard;
          if (sb != null)
            LayoutRoot.Resources.Remove(SLIDE_UP);

          sb = new Storyboard();
          LayoutRoot.Resources.Add(SLIDE_UP, sb);
          //sb.Duration = new Duration(TimeSpan.FromSeconds(seconds));

          TransformGroup tGroup = new TransformGroup();
          TranslateTransform tt = new TranslateTransform();
          tGroup.Children.Add(tt);
          control.RenderTransform = tGroup;
          control.RenderTransformOrigin = new Point(0.5, 0.5);

          DoubleAnimation dY = new DoubleAnimation();
          dY.From = control.ActualHeight;
          dY.To = 0;
          dY.FillBehavior = FillBehavior.HoldEnd;
          dY.Duration = new Duration(TimeSpan.FromSeconds(seconds));
          sb.Children.Add(dY);

          Storyboard.SetTarget(dY, control);
          Storyboard.SetTargetProperty(dY,
             new PropertyPath("(UIElement.RenderTransform).(tGroup.Children)[0].(TranslateTransform.Y)"));

          sb.Completed += (s, e) =>
          {
            sb.Stop();
          };
          return sb;
        }

        //private void SlideUp_Click(object sender, RoutedEventArgs e)
        //{
        //  Storyboard sb = PrepareSlideUpStoryBoard(imgPicture, 3);
        //  m.Source = imgThumb.Source;
        //  sb.Begin();
        //}

        #endregion Slide Up Storyboard

        #region Slide Down Storyboard

        private Storyboard PrepareSlideDownStoryBoard(Control control,double w,double h, double seconds)
        {
          Storyboard sb = null;
          if (LayoutRoot.Resources.Contains(SLIDE_UP))
            sb = LayoutRoot.Resources[SLIDE_UP] as Storyboard;
          if (sb != null)
            //return sb;
            LayoutRoot.Resources.Remove(SLIDE_UP);

          sb = new Storyboard();
          LayoutRoot.Resources.Add(SLIDE_UP, sb);
          //sb.Duration = new Duration(TimeSpan.FromSeconds(seconds));

          TransformGroup tGroup = new TransformGroup();
          TranslateTransform tt = new TranslateTransform();
          tGroup.Children.Add(tt);
          control.RenderTransform = tGroup;
          control.RenderTransformOrigin = new Point(0.5, 0.5);

          DoubleAnimation dY = new DoubleAnimation();
          dY.From = -control.ActualHeight;
          dY.To = 0;
          dY.Duration = new Duration(TimeSpan.FromSeconds(seconds));
          dY.FillBehavior = FillBehavior.HoldEnd;
          sb.Children.Add(dY);

          Storyboard.SetTarget(dY, control);
          Storyboard.SetTargetProperty(dY,
             new PropertyPath("(UIElement.RenderTransform).(tGroup.Children)[0].(TranslateTransform.Y)"));

          sb.Completed += (s, e) =>
          {
            sb.Stop();
          };
          return sb;
        }

        //private void SlideDown_Click(object sender, RoutedEventArgs e)
        //{
        //  Storyboard sb = PrepareSlideDownStoryBoard(imgPicture, 3);
        //  imgPicture.Source = imgThumb.Source;
        //  sb.Begin();

        //}

        #endregion Slide Down Storyboard

        #region Slide In From Right Storyboard

        private Storyboard PrepareSlideInFromRightStoryBoard(Control control, double width,double h, double seconds)
        {
          Storyboard sb = null;
          if (LayoutRoot.Resources.Contains(SLIDE_RIGHT))
            sb = LayoutRoot.Resources[SLIDE_RIGHT] as Storyboard;
          if (sb != null)
            //return sb;
            LayoutRoot.Resources.Remove(SLIDE_RIGHT);

          sb = new Storyboard();
          LayoutRoot.Resources.Add(SLIDE_RIGHT, sb);
          //sb.Duration = new Duration(TimeSpan.FromSeconds(seconds));

          TransformGroup tGroup = new TransformGroup();
          TranslateTransform tt = new TranslateTransform();
          tGroup.Children.Add(tt);
          control.RenderTransform = tGroup;
          control.RenderTransformOrigin = new Point(0.5, 0.5);

          DoubleAnimation dX = new DoubleAnimation();
          dX.From = width;
          dX.To = 0;
          dX.FillBehavior = FillBehavior.HoldEnd;
          dX.Duration = new Duration(TimeSpan.FromSeconds(seconds));

          sb.Children.Add(dX);

          Storyboard.SetTarget(dX, control);
          Storyboard.SetTargetProperty(dX,
             new PropertyPath("(UIElement.RenderTransform).(tGroup.Children)[0].(TranslateTransform.X)"));

          sb.Completed += (s, e) =>
          {
            sb.Stop();
          };
          return sb;
        }

        //private void SlideInRight_Click(object sender, RoutedEventArgs e)
        //{
        //  double width = this.LayoutRoot.ColumnDefinitions[0].ActualWidth;
        //  Storyboard sb = PrepareSlideInFromRightStoryBoard(imgPicture, 5, width);
        //  imgPicture.Source = imgThumb.Source;
        //  sb.Begin();
        //}

        #endregion Slide In From Right Storyboard

        #region Slide In From Left Storyboard

        private Storyboard PrepareSlideInFromLeftStoryBoard(Control control,  double width,double h,double seconds)
        {
          Storyboard sb = null;
          if (LayoutRoot.Resources.Contains(SLIDE_LEFT))
            sb = LayoutRoot.Resources[SLIDE_LEFT] as Storyboard;
          if (sb != null)
            //return sb;
            LayoutRoot.Resources.Remove(SLIDE_LEFT);

          sb = new Storyboard();
          LayoutRoot.Resources.Add(SLIDE_LEFT, sb);
          //sb.Duration = new Duration(TimeSpan.FromSeconds(seconds));

          TransformGroup tGroup = new TransformGroup();
          TranslateTransform tt = new TranslateTransform();
          tGroup.Children.Add(tt);
          control.RenderTransform = tGroup;
          control.RenderTransformOrigin = new Point(0.5, 0.5);

          DoubleAnimation dX = new DoubleAnimation();
          dX.From = -width;
          dX.To = 0;
          dX.FillBehavior = FillBehavior.HoldEnd;
          dX.Duration = new Duration(TimeSpan.FromSeconds(seconds));
          sb.Children.Add(dX);

          Storyboard.SetTarget(dX, control);
          Storyboard.SetTargetProperty(dX,
             new PropertyPath("(UIElement.RenderTransform).(tGroup.Children)[0].(TranslateTransform.X)"));

          sb.Completed += (s, e) =>
          {
            sb.Stop();
          };
          return sb;
        }

       



        #endregion Slide In From Left Storyboard

        private void ContentGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (this.ContentGrid.ActualWidth <= 800)
            {
                this.ContentGrid.Margin = new Thickness(50, 0, 0, 0);
            }
            else
            {
                this.ContentGrid.Margin = new Thickness(150, 0, 0, 0);
            }
        }

        private void btnSalaryDetail_Click(object sender, RoutedEventArgs e)
        {
            //"天水移动分公司社会渠道酬金管理办法.doc"
         //   btnSalaryDetail.NavigateUri = new Uri(System.Windows.Browser.HtmlPage.Document.DocumentUri, new Uri(@"Profile/天水移动分公司社会渠道酬金管理办法.doc", UriKind.Relative));


        }
        
    }
}

#region old   
       
    
    /*
    #region Fade Storyboard

    private Storyboard PrepareFadeStoryBoard(Image img, bool fadein, string key)
    {
      Storyboard sb = null;
      if (LayoutRoot.Resources.Contains(key))
        sb = LayoutRoot.Resources[key] as Storyboard;
      if (sb != null)
        //return sb;
        LayoutRoot.Resources.Remove(key);

      sb = new Storyboard();
      LayoutRoot.Resources.Add(key, sb);
      // the next line causes the storyboard to pause after the animation completes by the length of the animation
      //    before it ends
      //sb.Duration = new Duration(TimeSpan.FromSeconds(2));

      DoubleAnimation dFade = new DoubleAnimation();
      if (fadein)
      {
        dFade.From = 0;
        dFade.To = 1;
      }
      else
      {
        dFade.From = 1;
        dFade.To = 0;
        m_fadeTarget = img;
      }
      dFade.FillBehavior = FillBehavior.HoldEnd;
      dFade.Duration = new Duration(TimeSpan.FromSeconds(FADESECONDS));
      sb.Children.Add(dFade);
      Storyboard.SetTarget(dFade, img);
      Storyboard.SetTargetProperty(dFade,
         new PropertyPath("(UIElement.Opacity)"));

      if (fadein)
        sb.Completed += new EventHandler(Fade_InCompleted);
      else
        sb.Completed += new EventHandler(Fade_OutCompleted);
      return sb;
    }

    private void Fade_InCompleted(object sender, EventArgs e)
    {
      Storyboard sb = sender as Storyboard;
      if (sb != null)
      {
        sb.Stop();
        LayoutRoot.Resources.Remove(FADEIN);
        m_fadeTarget = null;
      }
    }

    private void Fade_OutCompleted(object sender, EventArgs e)
    {
      Storyboard sb = sender as Storyboard;
      //DoubleAnimation da = (DoubleAnimation)sb.Children[0];
      //string targetName = Storyboard.GetTargetName(da);
      //var target = LayoutRoot.Children.Single(c => ((FrameworkElement)c).Name == targetName);
      if (sb != null)
      {
        sb.Stop();
        LayoutRoot.Resources.Remove(FADEOUT);
      }
      sb = PrepareFadeStoryBoard(m_fadeTarget, true, FADEIN);
      m_fadeTarget.Source = imgThumb.Source;
      sb.Begin();
    }

    private void Fade_Click(object sender, RoutedEventArgs e)
    {
      Storyboard sb = PrepareFadeStoryBoard(imgPicture, false, FADEOUT);
      sb.Begin();
    }

    #endregion Fade Storyboard

    #region Wipe In Storyboard

    private Storyboard PrepareWipeInStoryBoard(Image img, double picWidth, double picHeight, double seconds)
    {
      Storyboard sb = null;
      if (LayoutRoot.Resources.Contains(WIPE_IN))
        sb = LayoutRoot.Resources[WIPE_IN] as Storyboard;
      if (sb != null)
        LayoutRoot.Resources.Remove(WIPE_IN);

      sb = new Storyboard();
      LayoutRoot.Resources.Add(WIPE_IN, sb);

      EllipseGeometry elGeom = new EllipseGeometry();
      elGeom.Center = new Point(picWidth / 2, picHeight / 2);
      imgPicture.Clip = elGeom;

      DoubleAnimation dX = new DoubleAnimation();
      dX.From = 0;
      dX.To = picWidth;
      dX.FillBehavior = FillBehavior.HoldEnd;
      dX.Duration = new Duration(TimeSpan.FromSeconds(seconds));
      
      sb.Children.Add(dX);

      Storyboard.SetTarget(dX, img);
      Storyboard.SetTargetProperty(dX,
         new PropertyPath("(UIElement.Clip).(EllipseGeometry.RadiusX)"));

      DoubleAnimation dY = new DoubleAnimation();
      dY.From = 0;
      dY.To = picHeight;
      dY.FillBehavior = FillBehavior.HoldEnd;
      dY.Duration = new Duration(TimeSpan.FromSeconds(seconds));
      sb.Children.Add(dY);

      Storyboard.SetTarget(dY, img);
      Storyboard.SetTargetProperty(dY,
         new PropertyPath("(UIElement.Clip).(EllipseGeometry.RadiusY)"));

      sb.Completed +=new EventHandler(WipeIn_Completed);
      return sb;
    }

    private void WipeIn_Completed(object sender, EventArgs e)
    {
      Storyboard sb = sender as Storyboard;
      if (sb != null)
      {
        sb.Stop();
        LayoutRoot.Resources.Remove(WIPE_IN);
        m_curAction = string.Empty;
      }
      imgPicture.Clip = null;
    }

    private void WipeIn_Click(object sender, RoutedEventArgs e)
    {      
      double oldRatio = imgPicture.ActualWidth / imgPicture.ActualHeight;
      double newRatio = imgThumb.ActualWidth / imgThumb.ActualHeight;
      imgPicture.Source = imgThumb.Source;
      double diff = Math.Abs(newRatio - oldRatio);
      if (diff < 0.01)
      {
        Storyboard sb = PrepareWipeInStoryBoard(imgPicture, imgPicture.ActualWidth, imgPicture.ActualHeight, 2);
        sb.Begin();
      }
      else
      {
        // do it in the image resize event
        m_curAction = WIPE_IN;
      }
    }

    #endregion Wipe In Storyboard

    #region Fill In From Left Storyboard

    private Storyboard PrepareFillInFromLeftStoryBoard(Image img, double width, double height, double seconds)
    {
      Storyboard sb = null;
      if (LayoutRoot.Resources.Contains(FILL_LEFT))
        sb = LayoutRoot.Resources[FILL_LEFT] as Storyboard;
      if (sb != null)
        LayoutRoot.Resources.Remove(FILL_LEFT);

      sb = new Storyboard();
      LayoutRoot.Resources.Add(FILL_LEFT, sb);

      PathGeometry pg = new PathGeometry();
      PathFigure pf = new PathFigure();
      pf.StartPoint = new Point(0, 0);
      pg.Figures.Add(pf);
      LineSegment ls1 = new LineSegment();
      ls1.Point = new Point(width, 0);
      pf.Segments.Add(ls1);
      LineSegment ls2 = new LineSegment();
      ls2.Point = new Point(width, height);
      pf.Segments.Add(ls2);
      LineSegment ls3 = new LineSegment();
      ls3.Point = new Point(0, height);
      pf.Segments.Add(ls3);
      pf.IsClosed = true;

      img.Clip = pg;

      PointAnimation px = new PointAnimation();
      px.From = new Point(0, 0);
      px.To = new Point(width, 0);
      px.FillBehavior = FillBehavior.HoldEnd;
      px.Duration = new Duration(TimeSpan.FromSeconds(seconds));
      sb.Children.Add(px);
    
      PointAnimation py = new PointAnimation();
      py.From = new Point(0, height);
      py.To = new Point(width, height);
      py.FillBehavior = FillBehavior.HoldEnd;
      py.Duration = new Duration(TimeSpan.FromSeconds(seconds));
      sb.Children.Add(py);

      Storyboard.SetTarget(px, ls1);
      Storyboard.SetTargetProperty(px,
         new PropertyPath("(Point)"));

      Storyboard.SetTarget(py, ls2);
      Storyboard.SetTargetProperty(py,
         new PropertyPath("(Point)"));

      sb.Completed += (s, e) =>
      {
        sb.Stop();
        LayoutRoot.Resources.Remove(FILL_LEFT);
        imgPicture.Clip = null;
      };
      return sb;
    }

    private void FillIn_Click(object sender, RoutedEventArgs e)
    {
      double oldRatio = imgPicture.ActualWidth / imgPicture.ActualHeight;
      double newRatio = imgThumb.ActualWidth / imgThumb.ActualHeight;
      imgPicture.Source = imgThumb.Source;
      double diff = Math.Abs(newRatio - oldRatio);
      if (diff < 0.01)
      {
        Storyboard sb = PrepareFillInFromLeftStoryBoard(imgPicture, imgPicture.ActualWidth, imgPicture.ActualHeight, 2);
        sb.Begin();
      }
      else
      {
        // do it in the image resize event
        m_curAction = FILL_LEFT;
      }
    }

    #endregion Fill In From Left Storyboard
         * */
#endregion

